home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / board / Chaos53src.lha / chaos / src / OutAmi.c < prev    next >
C/C++ Source or Header  |  1994-11-19  |  12KB  |  483 lines

  1. /*  Chaos:            The Chess HAppening Organisation System    V5.3
  2.     Copyright (C)   1993    Jochen Wiedmann
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.     $RCSfile: OutAmi.c,v $
  20.     $Revision: 3.2 $
  21.     $Date: 1994/11/19 19:32:01 $
  22.  
  23.     These are the system dependent output functions.
  24.  
  25.     Computer:    Amiga 1200            Compiler:    Dice 2.07.54 (3.0)
  26.  
  27.     Author:    Jochen Wiedmann
  28.         Am Eisteich 9
  29.       72555 Metzingen
  30.         Tel. 07123 / 14881
  31.         Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
  32. */
  33.  
  34.  
  35. #ifndef CHAOS_H
  36. #include "chaos.h"
  37. #endif
  38.  
  39. #ifdef AMIGA
  40. #ifndef EXEC_EXECBASE_H
  41. #include <exec/libraries.h>
  42. #endif
  43. #ifndef PREFS_PREFHDR_H
  44. #include <prefs/prefhdr.h>
  45. #endif
  46. #ifndef PREFS_PRINTERTXT_H
  47. #include <prefs/printertxt.h>
  48. #endif
  49. #ifndef CLIB_IFFPARSE_PROTOS_H
  50. #include <clib/iffparse_protos.h>
  51. #endif
  52.  
  53. #ifdef AZTEC
  54. #ifndef __PRAMASE_IFFPARSE_LIB_H
  55. #include <pragmas/iffparse_lib.h>
  56. #endif
  57. #endif    /*  AZTEC   */
  58.  
  59. #if defined(_DCC)  ||  defined(__SASC)  ||  defined(__MAXON__)
  60. #ifndef PRAMASE_IFFPARSE_PRAGMAS_H
  61. #include <pragmas/iffparse_pragmas.h>
  62. #endif
  63. #endif    /*  _DCC  ||  __SASC  ||  __MAXON   */
  64.  
  65. #ifdef __GNUC__
  66. #ifndef _INLINE_IFFPARSE_H
  67. #include <inline/iffparse.h>
  68. #endif
  69. #endif    /*  __GNUC__    */
  70.  
  71. #endif    /*  AMIGA   */
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.     The following function makes text being centered on the printer.
  78.     This isn't needed at all and might be a stub function on other systems
  79.     than the Amiga.
  80.  
  81.     Inputs: device - the device to which the output goes
  82.  
  83.     Results: TRUE, if successfull, FALSE otherwise
  84. */
  85. int CenterText(int device)
  86.  
  87. #ifdef AMIGA    /*  The following lines produce centered output */
  88. {
  89.   if ((device == DEVICE_PrinterLQ  ||  device == DEVICE_PrinterDraft)  &&
  90.       !lprint("\2331 F"))
  91.   { return(FALSE);
  92.   }
  93.   return(TRUE);
  94. }
  95. #endif    /*  AMIGA   */
  96.  
  97.  
  98.  
  99.  
  100. /*
  101.     The InitOutput() function gets called by output functions before they
  102.     do any output via lprint() or longlprint().
  103.  
  104.     Inputs: title     - the printed document's title
  105.         prthead   - the head to print above each page (printer output)
  106.         scrhead   - the head to appear on top of the output window
  107.             (screen output)
  108.         filename  - the destination file (used if output to file only);
  109.             this can be NULL, in which case InitOutput() brings
  110.             up a filerequester asking for the filename
  111.         device    - the output device: DEVICE_Screen, DEVICE_PrinterDraft,
  112.             DEVICE_PrinterLQ, Device_FileAscii or DEVICE_FileTeX
  113.         prtlines  - the number of lines, that one item needs on the
  114.             printer
  115.         scrlines  - the number of lines, that one item needs on the
  116.             screen
  117.  
  118.     Result: TRUE, if successfull, FALSE otherwise
  119. */
  120. static char *titleOutput;
  121. static char *prtheadOutput;
  122. static char *scrheadOutput;
  123. static char *filenameOutput;
  124. static int deviceOutput;
  125. static int prtlinesOutput;
  126. static int scrlinesOutput;
  127. void *OutputMemList;
  128. struct MinList OutputList;
  129. char *OutputLine;
  130. int OutputLineLen;
  131. int OutputLineMaxLen;
  132. int OutputReturnCode;
  133.  
  134. int InitOutput(char *title, char *prthead, char *scrhead, char *filename,
  135.            char *ending, int device, int prtlines, int scrlines)
  136.  
  137. #ifdef AMIGA
  138. {
  139.   prtlinesOutput = prtlines;
  140.   scrlinesOutput = scrlines;
  141.  
  142.   OutputReturnCode = 0;
  143.   OutputMemList = NULL;
  144.   OutputLine = NULL;
  145.   OutputLineLen = OutputLineMaxLen = (device == DEVICE_Screen) ? 0 : -1;
  146.   deviceOutput = device;
  147.   NewList((struct List *) &OutputList);
  148.  
  149.   if ((device == DEVICE_FileAscii  ||  device == DEVICE_FileTeX)  &&
  150.       filename == NULL)
  151.   { if ((filename = FileRequest(NULL,
  152.                 (char *) GetChaosString(WND_ASCIIFILE_TITLE),
  153.                 (device == DEVICE_FileTeX) ? "#?.tex" : ending,
  154.                 TRUE))
  155.           ==  NULL)
  156.     { OutputReturnCode = RETURN_WARN;
  157.       return(FALSE);
  158.     }
  159.   }
  160.  
  161.   if (!(titleOutput = GetStringMem(&OutputMemList, title))  ||
  162.       !(prtheadOutput = GetStringMem(&OutputMemList, prthead))  ||
  163.       !(scrheadOutput = GetStringMem(&OutputMemList, scrhead))  ||
  164.        (filename != NULL  &&
  165.     !(filenameOutput = GetStringMem(&OutputMemList, filename))))
  166.   { TerminateOutput();
  167.     OutputReturnCode = RETURN_ERROR;
  168.     return(FALSE);
  169.   }
  170.   return(TRUE);
  171. }
  172. #endif    /*  AMIGA   */
  173.  
  174.  
  175.  
  176.  
  177. /*
  178.     TerminateOutput() gets called from the output functions if an error
  179.     occurs or if the output is ended.
  180. */
  181. void TerminateOutput(void)
  182.  
  183. #ifdef AMIGA
  184. {
  185.   PutMemList(&OutputMemList);
  186. }
  187. #endif    /*  AMIGA   */
  188.  
  189.  
  190.  
  191.  
  192. /*
  193.     ProcessOutput() gets called, if a list of lines to be printed is setup
  194.     with lprint or "by hand". (see OutRound)
  195.  
  196.     Inputs: list -  list of lines to be printed; this can be NULL, in which
  197.             case OutputList is assumed
  198. */
  199. void ProcessOutput(void)
  200.  
  201. #ifdef AMIGA
  202. { struct MinNode *line;
  203.  
  204.  
  205.   /*
  206.       Output to a file is rather simple...
  207.   */
  208.   if (deviceOutput == DEVICE_FileAscii  ||  deviceOutput == DEVICE_FileTeX)
  209.   { FILE *fh;
  210.  
  211.     if ((fh = fopen(filenameOutput, "w"))  ==  NULL)
  212.     { ShowError((char *) GetChaosString(MSG_NO_WRITE_FILE),
  213.         filenameOutput, IoErr());
  214.       OutputReturnCode = RETURN_ERROR;
  215.       return;
  216.     }
  217.  
  218.     if (deviceOutput == DEVICE_FileAscii  &&
  219.     (fputs(TrnName, fh) < 0  ||  fputs("\n\n", fh) < 0  ||
  220.      fputs(titleOutput, fh) < 0  ||  fputs("\n\n", fh) < 0  ||
  221.      fputs(prtheadOutput, fh) < 0  ||  fputs("\n\n", fh) < 0))
  222.     { OutputReturnCode = RETURN_ERROR;
  223.     }
  224.     else
  225.     { for(line = OutputList.mlh_Head;  line->mln_Succ != NULL;
  226.       line = line->mln_Succ)
  227.       { if (fputs((char *) (line+1), fh) < 0  ||
  228.         fputc('\n', fh) == EOF)
  229.     { OutputReturnCode = RETURN_ERROR;
  230.       break;
  231.     }
  232.       }
  233.     }
  234.     fclose(fh);
  235.   }
  236.  
  237.   /*
  238.       Output to the printer is a little bit more complicated, because it
  239.       is page oriented. The largest problem is to get the paper length.
  240.       Sigh! Why are the new preferences that complicated?
  241.   */
  242.   else if (deviceOutput != DEVICE_Screen)
  243.   { int paperlen, pagenr, lines;
  244.     FILE *fh;
  245.     int j, writeerr;
  246.  
  247.     /*
  248.     Get the paperlength
  249.     */
  250.     struct Preferences Prefs;
  251.  
  252.     GetPrefs(&Prefs, sizeof(Prefs));
  253.     paperlen = Prefs.PaperLength;
  254. #ifdef V39_INCLUDES
  255.     { struct IFFHandle *iffhandle;
  256.       struct StoredProperty *sp;
  257.       int ifferr;
  258.       extern struct Library *IFFParseBase;
  259.       extern struct Library *SysBase;
  260.  
  261.       if(IFFParseBase != NULL  &&  SysBase->lib_Version >= 37)
  262.       { if ((iffhandle = AllocIFF())  !=  NULL)
  263.     { if ((iffhandle->iff_Stream = Open((STRPTR) "ENV:SYS/Printer.prefs",
  264.                         MODE_OLDFILE))
  265.                      !=  NULL)
  266.       { InitIFFasDOS(iffhandle);
  267.         if (OpenIFF(iffhandle, IFFF_READ)  ==  0)
  268.         { if (PropChunk(iffhandle, ID_PREF, ID_PTXT)  ==  0)
  269.           { for(;;)
  270.         { ifferr = ParseIFF(iffhandle, IFFPARSE_STEP);
  271.           if (ifferr  !=  0)
  272.           { if (ifferr  ==  IFFERR_EOC)
  273.             { continue;
  274.             }
  275.             else
  276.             { break;
  277.             }
  278.           }
  279.  
  280.           if ((sp = FindProp(iffhandle, ID_PREF, ID_PTXT))
  281.               !=  NULL)
  282.           { paperlen = ((struct PrinterTxtPrefs *)
  283.                    (sp->sp_Data))->pt_PaperLength;
  284.             break;
  285.           }
  286.         }
  287.           }
  288.           CloseIFF(iffhandle);
  289.         }
  290.         Close(iffhandle->iff_Stream);
  291.       }
  292.       FreeIFF(iffhandle);
  293.     }
  294.       }
  295.     }
  296. #endif
  297.  
  298.  
  299.     if ((fh = fopen("prt:", "w"))  ==  NULL)
  300.     { ShowError((char *) GetChaosString(MSG_NO_PRINTER));
  301.       OutputReturnCode = RETURN_ERROR;
  302.       return;
  303.     }
  304.     writeerr = TRUE;
  305.  
  306.     /*
  307.     Put application into sleep mode.
  308.     */
  309.     set(App, MUIA_Application_Sleep, TRUE);
  310.  
  311.     /*
  312.     Put printer into draft or LQ mode
  313.     */
  314.     if (fprintf(fh, "%s",
  315.         (deviceOutput == DEVICE_PrinterDraft)  ?
  316.             "\2331\"z" : "\2332\"z")    <  0)
  317.     { goto WriteError;
  318.     }
  319.  
  320.  
  321.     line = OutputList.mlh_Head;
  322.     pagenr = 0;
  323.     while (line->mln_Succ != NULL)
  324.     {
  325.       if (fprintf(fh, "%s\n\n", TrnName)  <  0                  ||
  326.       fprintf(fh, "%-65s  %7s %d\n\n", titleOutput,
  327.           GetChaosString(MSG_PAGENR), ++pagenr)  <  0   ||
  328.       fprintf(fh, "%s\n\n", prtheadOutput)  <  0)
  329.       { goto WriteError;
  330.       }
  331.       lines = 7 + prtlinesOutput;
  332.  
  333.       while (lines+prtlinesOutput < paperlen  &&  line->mln_Succ != NULL)
  334.       { lines += prtlinesOutput;
  335.     for (j = 0;  j < prtlinesOutput;  j++)
  336.     { if (fprintf(fh, "%s\n", line+1)  <  0)
  337.       { goto WriteError;
  338.       }
  339.       line = line->mln_Succ;
  340.     }
  341.       }
  342.  
  343.       while (lines++ < paperlen)
  344.       { if (fprintf(fh, "\n")  <  0)
  345.     { goto WriteError;
  346.     }
  347.       }
  348.  
  349.       if (fprintf(fh, "\n%s\n", PVERSION)  <  0     ||
  350.       fprintf(fh, "\f")  <  0)
  351.       { goto WriteError;
  352.       }
  353.     }
  354.     writeerr = FALSE;
  355.  
  356. WriteError:
  357.     fclose(fh);
  358.     if (writeerr)
  359.     { ShowError((char *) GetChaosString(MSG_PrinterError));
  360.     }
  361.  
  362.     /*
  363.     Awake application
  364.     */
  365.     set(App, MUIA_Application_Sleep, FALSE);
  366.   }
  367.  
  368.   /*
  369.       At last output to the screen, which isn't very complicated too.
  370.       Thanks MUI.
  371.   */
  372.   else
  373.   { ULONG open, signal;
  374.     APTR OutWnd;    /*  output window            */
  375.     APTR OutWnd_Head;    /*  page head gadget            */
  376.     APTR OutWnd_LV;    /*  output text gadget            */
  377.  
  378.     OutWnd = WindowObject,
  379.         MUIA_Window_ID, MAKE_ID('O','U','T','\0'),
  380.         MUIA_Window_Width, MUIV_Window_Width_MinMax(70),
  381.         MUIA_Window_Height, MUIV_Window_Height_MinMax(30),
  382.         MUIA_Window_Title, titleOutput,
  383.         WindowContents, VGroup,
  384.             Child, OutWnd_Head = TextObject,
  385.             MUIA_Text_Contents, scrheadOutput,
  386.             ReadListFrame,
  387.             MUIA_Font, MUIV_Font_Fixed,
  388.             End,
  389.             Child, OutWnd_LV = ListviewObject,
  390.             MUIA_Listview_List, FloattextObject,
  391.                 MUIA_Floattext_Text, OutputLine,
  392.                 ReadListFrame,
  393.                 MUIA_Font, MUIV_Font_Fixed,
  394.             End,
  395.             End,
  396.         End,
  397.         End;
  398.  
  399.     if (!OutWnd)
  400.     { return;
  401.     }
  402.  
  403.  
  404.     /*
  405.     Add the new window as a member to the application.
  406.     Setting up the notification events for the output window:
  407.     CloseWindow gadget only
  408.     */
  409. #define ID_OutWnd_Cancel 100
  410.     DoMethod(App, OM_ADDMEMBER, OutWnd);
  411.     DoMethod(OutWnd, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
  412.          App, 2, MUIM_Application_ReturnID, ID_OutWnd_Cancel);
  413.  
  414.     /*
  415.     Open the window
  416.     */
  417.     set(OutWnd, MUIA_Window_Open, TRUE);
  418.     get(OutWnd, MUIA_Window_Open, &open);
  419.     if (!open)
  420.     { MUIError((char *) GetChaosString(ERRMSG_CANNOT_OPEN_WINDOW));
  421.       DoMethod(App, OM_REMMEMBER, OutWnd);
  422.       MUI_DisposeObject(OutWnd);
  423.       return;
  424.     }
  425.  
  426.     /*
  427.     Close the main window while the output window is open
  428.     */
  429.     set(MainWnd, MUIA_Window_Open, FALSE);
  430.  
  431.     /*
  432.     Wait for user actions
  433.     */
  434.     for(;;)
  435.     { switch(DoMethod(App, MUIM_Application_Input, &signal))
  436.       { case MUIV_Application_ReturnID_Quit:
  437.       if (TestSaved())
  438.       { exit(0);
  439.       }
  440.       break;
  441.     case ID_OutWnd_Cancel:
  442.       set(OutWnd, MUIA_Window_Open, FALSE);
  443.       DoMethod(App, OM_REMMEMBER, OutWnd);
  444.       MUI_DisposeObject(OutWnd);
  445.       return;
  446.       }
  447.  
  448.       if (signal)
  449.       { Wait(signal);
  450.       }
  451.     }
  452.   }
  453. }
  454. #endif    /*  AMIGA   */
  455.  
  456.  
  457.  
  458.  
  459. /*
  460.     AskForBirthday() should bring up a requester and ask for the birthday
  461.     of a player whose birthday field isn't set or valid.
  462.  
  463.     Inputs: plr - the player who is asked for
  464.  
  465.     Results: 1 = Assume that player is 20 or younger
  466.          2 = Assume that player is between 21 and 25 years old
  467.          3 = Assume that player is 26 or older
  468.          4 = The player wants to modify the players data
  469.          5 = Skip this player (will not be present in the DWZ report)
  470.          0 = Cancel
  471. */
  472. int AskForBirthday(struct Player *plr)
  473.  
  474. #ifdef AMIGA
  475. {
  476.   return(MUI_Request(App, MainWnd, 0,
  477.              (char *) GetChaosString(MSG_ATTENTION),
  478.              (char *) GetChaosString(MSG_NO_BIRTHDAY_GADGETS),
  479.              (char *) GetChaosString(MSG_NO_BIRTHDAY),
  480.              plr->Name));
  481. }
  482. #endif    /*  AMIGA   */
  483.